package com.wuxiaolong;

import com.wuxiaolong.wechat.Application;
import com.wuxiaolong.wechat.service.AccessTokenService;
import com.wuxiaolong.wechat.service.impl.AccessTokenServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.web.client.RestTemplate;

import java.nio.charset.Charset;
import java.util.List;

/**
 * Description:
 *
 * @author Leo
 * @date 2020-02-13
 */
@RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class)
@Slf4j
public class TemplateMessage {

    @Autowired
    private AccessTokenService accessTokenService;


    /**
     * 设置所属行业
     */
    @Test
    public void configIndustry(){
        RestTemplate restTemplate = new RestTemplate();
        String url = "https://api.weixin.qq.com/cgi-bin/template/api_set_industry?access_token=ACCESS_TOKEN";
        String tempUrl = url.replace("ACCESS_TOKEN",accessTokenService.getAccessToken());
        String data = "{\"industry_id1\":\"1\",\"industry_id2\":\"2\"}";

        String resp = restTemplate.postForObject(tempUrl,data,String.class);

        //resp:{"errcode":0,"errmsg":"ok"}
        log.info("resp:{}",resp);
    }

    /**
     * 查询设置的所属行业
     */
    @Test
    public void queryConfigIndustry(){
        RestTemplate restTemplate = new RestTemplate();
        String url = "https://api.weixin.qq.com/cgi-bin/template/get_industry?access_token=ACCESS_TOKEN";
        String tempUrl = url.replace("ACCESS_TOKEN",accessTokenService.getAccessToken());

        //解决中文乱码
        List<HttpMessageConverter<?>> list = restTemplate.getMessageConverters();
        for (HttpMessageConverter<?> httpMessageConverter : list) {
            if (httpMessageConverter instanceof StringHttpMessageConverter) {
                ((StringHttpMessageConverter) httpMessageConverter).setDefaultCharset(Charset.forName("UTF-8"));
                break;
            }
        }


        String resp = restTemplate.getForObject(tempUrl,String.class);
        log.info("resp:{}",resp);
    }

    /**
     * 发送模板消息
     */
    @Test
    public void sendTemplateMessage(){
        RestTemplate restTemplate = new RestTemplate();
        String url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=ACCESS_TOKEN";
        String tempUrl = url.replace("ACCESS_TOKEN",accessTokenService.getAccessToken());

        String data = "{\n" +
                "           \"touser\":\"oP78AwXH2Xyl-Yzs5SOyrOD94qj0\",\n" +
                "           \"template_id\":\"CPO3mnvOatheDqRGwWgmLNcavEwkUdWcuaX-MhMMtHo\",\n" +
                "           \"url\":\"http://wuxiaolong.tech\",  \n" +
//                "           \"miniprogram\":{\n" +
//                "             \"appid\":\"xiaochengxuappid12345\",\n" +
//                "             \"pagepath\":\"index?foo=bar\"\n" +
//                "           },          \n" +
                "           \"data\":{\n" +
                "                   \"first\": {\n" +
                "                       \"value\":\"你是傻吊吗！\",\n" +
                "                       \"color\":\"#173177\"\n" +
                "                   },\n" +
                "                   \"company\":{\n" +
                "                       \"value\":\"你是傻吊吗！\",\n" +
                "                       \"color\":\"#173177\"\n" +
                "                   },\n" +
                "                   \"time\": {\n" +
                "                       \"value\":\"你是傻吊吗！\",\n" +
                "                       \"color\":\"#173177\"\n" +
                "                   },\n" +
                "                   \"result\": {\n" +
                "                       \"value\":\"你是傻吊吗！\",\n" +
                "                       \"color\":\"#173177\"\n" +
                "                   },\n" +
                "                   \"remark\":{\n" +
                "                       \"value\":\"你是傻吊吗！\",\n" +
                "                       \"color\":\"#173177\"\n" +
                "                   }\n" +
                "           }\n" +
                "       }";

        //解决中文乱码
        List<HttpMessageConverter<?>> list = restTemplate.getMessageConverters();
        for (HttpMessageConverter<?> httpMessageConverter : list) {
            if (httpMessageConverter instanceof StringHttpMessageConverter) {
                ((StringHttpMessageConverter) httpMessageConverter).setDefaultCharset(Charset.forName("UTF-8"));
                break;
            }
        }



        String resp = restTemplate.postForObject(tempUrl,data,String.class);

        //  resp:{"errcode":0,"errmsg":"ok","msgid":1208163989306359808}
        log.info("resp:{}",resp);
    }
}
